import React from 'react';
import { inject, observer } from 'mobx-react';
import { Button, Modal } from 'antd';
import { toJS } from "mobx";
import { WeaTableNew, WeaSwitch } from 'comsMobx';
import { WeaTab, WeaErrorPage, WeaTools, WeaDialog, WeaFormItem, WeaSearchGroup, WeaNewScroll } from 'ecCom';
import PlanInfoDialog from "./PlanInfoDialog"
import PlanMainDialog from "./PlanMainDialog"
const WeaTable = WeaTableNew.WeaTable;
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@inject('cptInventoryStore')
@observer
class PlanInfoTab extends React.Component {
    constructor(props){
        super(props);
        this.formParams = {};
    }

    componentDidMount() {
        this.doInit(this.props);
    }
    doInit(props) {
        const { cptInventoryStore } = props;
    }
    componentWillReceiveProps(nextProps) {

    }
    componentWillUnmount() {
        const { cptInventoryStore } = this.props;
        cptInventoryStore.clearStatus();
    }
    render() {
        const { cptInventoryStore } = this.props;
        const { showMainSearchAd, mainform, searchMainParams, tabvisible, maintitle } = cptInventoryStore;
        const topTab = [
            { title: getLabel(2121, '详细信息'), key: "0" },
            { title: getLabel(384634, "盘点总表"), key: "1" },
        ];
        let dialogHeight = window.innerHeight;
        let dialogWidth = 1200;
        if (dialogHeight > 768) {
            dialogHeight = 600;
        }else{
            dialogHeight = 500;
            dialogWidth = 1000;
        }

        return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@z8hstd`}
                title={maintitle}
                visible={tabvisible}
                icon="icon-coms-fa"
                iconBgcolor="#f14a2d"
                onCancel={() => { this.props.contentStore.handleDialog(false); }}
                style={{ width: dialogWidth, height: dialogHeight }}
            >
                <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@yu4991`}
                    datas={topTab}
                    keyParam="key"  //主键
                    selectedKey={searchMainParams.tabkey}
                    buttonsAd={searchMainParams.tabkey == "1" && this.getButtons(cptInventoryStore)}
                    searchType={searchMainParams.tabkey == "1" && ['advanced']}
                    setShowSearchAd={bool => { cptInventoryStore.setMainSearchAd(bool) }}
                    hideSearchAd={() => cptInventoryStore.setMainSearchAd(false)}
                    buttons={this.getTopButtons()}
                    buttonSpace={10}
                    searchsAd={
                        <div>{this.getCondition()}</div>
                    }
                    showSearchAd={showMainSearchAd}
                    onChange={(key) => { cptInventoryStore.changeTab(key) }}
                />
                {searchMainParams.tabkey == "1" ?
                    <PlanMainDialog ecId={`${this && this.props && this.props.ecId || ''}_PlanMainDialog@i5t1zj`} contentStore={cptInventoryStore} />
                    : <PlanInfoDialog ecId={`${this && this.props && this.props.ecId || ''}_PlanInfoDialog@5vstgx`} contentStore={cptInventoryStore} />}
            </WeaDialog>
        )
    }

    getTopButtons() {
        const { setVisiable, addlistVisible, plantableStore, canEditlist, inventorystate,selectid, canExcelOut, planmaintableStore, searchMainParams, exportMain,handleDialog_imp } = this.props.cptInventoryStore;
        const selectedRowKeys = plantableStore.selectedRowKeys;//得到选中数据的id
        const selectedRowKeys2 = planmaintableStore.selectedRowKeys;//得到选中数据的id
        const selectedLen = selectedRowKeys.length;
        const selectedLen2 = selectedRowKeys2.length;

        let btnArr = [];
        let that = this;
        if (canEditlist && searchMainParams.tabkey == 0) {
            btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@g7vjmm@add`} type="primary" onClick={() => {
                    this.props.cptInventoryStore.getAddPlanlistForm({ id: selectid });
                    if (!addlistVisible) {
                        setVisiable(true, 3)
                    }
                }}> {getLabel(82, '新建')} </Button>
            );
            if(inventorystate=='0'){
                btnArr.push(
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@okg9ax@batchImport`} type="primary" onClick={() => {
                        handleDialog_imp(true, { id: selectid, name: "" }, 0);
                    }}> {getLabel(26601, '批量导入')} </Button>
                );
            }
            btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@dvto09@batchDelete`} type="primary"
                    disabled={selectedLen == 0}
                    onClick={() => {
                        const count = toJS(selectedRowKeys).length;
                        Modal.confirm({
                            title: getLabel(15172, '系统提示'),
                            content: getLabel(83600, '您确认要删除吗?'),
                            onOk() {
                                that.props.cptInventoryStore.doDeleteList(selectedRowKeys);
                            },
                            onCancel() { },
                        });
                    }}> {getLabel(32136, '批量删除')} </Button>
            );
        }

        if (searchMainParams.tabkey == 1) {
            if (canEditlist) {
                btnArr.push(
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@as7eiz@batchDelete`} type="primary"
                        disabled={selectedLen2 == 0}
                        style={{ marginRight: canExcelOut?"15px":"0px" }}
                        onClick={() => {
                            Modal.confirm({
                                title: getLabel(15172, '系统提示'),
                                content: getLabel(83600, '您确认要删除吗?'),
                                onOk() {
                                    that.props.cptInventoryStore.doDeleteMainCpt(selectedRowKeys2);
                                },
                                onCancel() { },
                            });
                        }}> {getLabel(32136, '批量删除')} </Button>
                );
            }
            if (!canExcelOut) {
                btnArr.push(
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@r6mgor@export`} type="primary"
                        style={{ marginRight: !canExcelOut?"15px":"0px" }}
                        onClick={() => {
                            exportMain();
                        }}> {getLabel(28343, '导出Excel')} </Button>
                );
            }
        }

        return btnArr;
    }


    getCondition = () => {//获取高级查询
        const { conditionMain, mainform, doPlanMainSearch, setMainSearchAd } = this.props.cptInventoryStore;
        const { isFormInit } = mainform;
        let group = [];
        const formParamsTemp = mainform.getFormParams();
        for(let param in formParamsTemp){
            this.formParams[param] = formParamsTemp[param];
        }
        isFormInit && conditionMain.map((c,i) => {
            let items = [];
            c.items.map((fields, index) => {
                items.push({
                    com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@nl2ve0@${fields.domkey[0]}`}
                        label={`${fields.label}`}
                        labelCol={{ span: `${fields.labelcol}` }}
                        wrapperCol={{ span: `${fields.fieldcol}` }}>
                        <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@7q658x@${fields.domkey[0]}`} fieldConfig={fields} form={mainform} formParams={this.formParams} onEnterSearch={this.onEnterSearch} />
                    </WeaFormItem>),
                    colSpan: 1,
                })
            });
            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@q0h4ie@${i}`} needTigger={true} title={c.title} showGroup={c.defaultshow} items={items} />)
        });
        return group;
    }

    getButtons = (cptInventoryStore) => {
        const { doPlanMainSearch, setMainSearchAd, clearMainFormFields } = cptInventoryStore;
        return [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@rqqoc6@search`} type="primary" onClick={() => { doPlanMainSearch(); setMainSearchAd(false); }}>{getLabel(197, "搜索")}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@nk009l@reset`} type="ghost" onClick={() => { clearMainFormFields(); }}>{getLabel(2022, "重置")}</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@7zrkux@close`} type="ghost" onClick={() => { setMainSearchAd(false) }}>{getLabel(201, "取消")}</Button>)
        ];
    }

    onEnterSearch = () =>{
        const { doPlanMainSearch, setMainSearchAd } = this.props.cptInventoryStore;
        doPlanMainSearch(); 
        setMainSearchAd(false);
    }

}

export default WeaTools.tryCatch(React,
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@3oze2d`} msg={props.error ? props.error : '对不起，该页面异常，请联系管理员！'} />,
    { error: "" }
)(PlanInfoTab);